package cn.com.hhrcw.conferenceRoomBatch.web;

import cn.com.hhrcw.conferenceRoomBatch.entity.ConferenceRoomBatch;
import cn.com.hhrcw.conferenceRoomBatch.mapstruct.ConferenceRoomBatchConvert;
import cn.com.hhrcw.conferenceRoomBatch.params.ConferenceRoomBatchParams;
import cn.com.hhrcw.conferenceRoomBatch.service.IConferenceRoomBatchService;
import cn.com.hhrcw.conferenceRoomBatch.vo.ConferenceRoomBatchVO;

import cn.com.hhrcw.entity.OpInterviewHistory;
import cn.com.hhrcw.service.IOpInterviewHistoryService;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.base.entity.AttributesValueParams;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;
import java.util.function.Function;

/**
* 联系人
* @module zyzg-boot-user
* @author shengli
* @since 2021-09-23
*/
@RestController
@Slf4j
@RequestMapping("/admin/conferenceRoomBatch")
@Validated
public class ConferenceRoomBatchController extends JeecgController<ConferenceRoomBatch, IConferenceRoomBatchService> {

    @Autowired
    private IConferenceRoomBatchService conferenceRoomBatchService;

    @Autowired
    private ConferenceRoomBatchConvert conferenceRoomBatchConvert;


    /**
    * 分页列表查询
    *
    * @param params 请求参数
    * @return
    */
    @GetMapping(value = "/list")
    @AutoLog("联系人-分页列表查询")
    @PermissionData
    public Result<Page<ConferenceRoomBatchVO>> list(ConferenceRoomBatchParams params, HttpServletRequest request) {
        QueryWrapper<ConferenceRoomBatch> queryWrapper = QueryGenerator.initQueryWrapper(conferenceRoomBatchConvert.toEntity(params), request.getParameterMap());
        queryWrapper.eq("del_flag", false);
        IPage<ConferenceRoomBatch> page = conferenceRoomBatchService.page(params.page(), queryWrapper);
        //分页结果转换器
        Function<ConferenceRoomBatch, ConferenceRoomBatchVO> fun = conferenceRoomBatch -> conferenceRoomBatchConvert.toVO(conferenceRoomBatch);

        return Result.success((Page)page.convert(fun));
    }

    /**
    * 根据id查询
    *
    * @param id 数据id
    * @return
    */
    @GetMapping(value = "/queryById")
    @PermissionData
    public Result<ConferenceRoomBatchVO> queryById(String id) {
        ConferenceRoomBatch conferenceRoomBatch = conferenceRoomBatchService
            .lambdaQuery()
            .eq(ConferenceRoomBatch::getId,id)
            .eq(ConferenceRoomBatch::getDelFlag,false)
            .one();
        Assert.notNull(conferenceRoomBatch, "数据不存在");
        ConferenceRoomBatchVO conferenceRoomBatchVO = conferenceRoomBatchConvert.toVO(conferenceRoomBatch);
        QueryWrapper<OpInterviewHistory> opInterviewHistoryQueryWrapper = Wrappers.<OpInterviewHistory>query().select("max(interview_no)");
        opInterviewHistoryQueryWrapper.eq("batch_id", id);
        Integer obj = opInterviewHistoryService.getObj(opInterviewHistoryQueryWrapper, e -> Convert.toInt(e));
        conferenceRoomBatchVO.setMaxNo(null == obj ? 1 : ++obj);
        return Result.success(conferenceRoomBatchVO);
    }

    @Autowired
    private IOpInterviewHistoryService opInterviewHistoryService;
    /**
    * 添加
    *
    * @param params 请求参数
    * @return
    */
    @PostMapping(value = "/add")
    @AutoLog("联系人-添加")
    @RequiresPermissions("conferenceRoomBatch:add")
    public Result<?> add(@RequestBody @Valid ConferenceRoomBatchParams params) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        ConferenceRoomBatch conferenceRoomBatch = conferenceRoomBatchConvert.toEntity(params);
        conferenceRoomBatch.setCreateBy(sysUser.getUsername());
        conferenceRoomBatch.setCreateTime(DateUtil.date());
        conferenceRoomBatch.setDelFlag(false);
        boolean saved = conferenceRoomBatchService.save(conferenceRoomBatch);
        return saved ? Result.ok() : Result.error("添加失败");
    }

    /**
    * 编辑
    *
    * @param params 请求参数
    * @return
    */
    @PostMapping(value = "/edit")
    @AutoLog("联系人-编辑")
    @RequiresPermissions("conferenceRoomBatch:edit")
    public Result<?> edit(@RequestBody @Valid ConferenceRoomBatchParams params) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        ConferenceRoomBatch conferenceRoomBatch = conferenceRoomBatchConvert.toEntity(params);
        conferenceRoomBatch.setUpdateBy(sysUser.getUsername());
        conferenceRoomBatch.setUpdateTime(DateUtil.date());
        boolean updated = conferenceRoomBatchService.updateById(conferenceRoomBatch);
        return updated ? Result.ok() : Result.error("编辑失败");
    }


    /**
    * 批量删除
    *
    * @param id 数据id
    * @return
    */
    @GetMapping(value = "/delete")
    @AutoLog("联系人-批量删除")
    @RequiresPermissions("conferenceRoomBatch:batch_del")
    public Result delete(String[] id) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        LambdaUpdateWrapper<ConferenceRoomBatch> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.in(ConferenceRoomBatch::getId, id)
            .set(ConferenceRoomBatch::getDelFlag, true)
            .set(ConferenceRoomBatch::getUpdateBy, sysUser.getUsername())
            .set(ConferenceRoomBatch::getUpdateTime, DateUtil.date());

        boolean deleted = conferenceRoomBatchService.update(updateWrapper);
        return deleted ? Result.ok() : Result.error("删除失败");
    }


    /**
    * 导出excel
    *
    * @param request
    * @param params
    */
    @SneakyThrows
    @GetMapping(value = "/exportXls")
    public Result exportXls(HttpServletRequest request, ConferenceRoomBatchParams params) {
        ConferenceRoomBatch conferenceRoomBatch = conferenceRoomBatchConvert.toEntity(params);
        conferenceRoomBatch.setDelFlag(false);
        QueryWrapper<ConferenceRoomBatch> queryWrapper = QueryGenerator.initQueryWrapper(conferenceRoomBatch, request.getParameterMap());
        List<ConferenceRoomBatch> data = conferenceRoomBatchService.list(queryWrapper);
        ExportParams exportParams = new ExportParams("联系人", "联系人");
        String url = super.exportXls(exportParams, ConferenceRoomBatchVO.class,conferenceRoomBatchConvert.toVO(data));
        return Result.ok((Object) url);
    }

    /**
    * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @PostMapping(value = "/importExcel")
        public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ConferenceRoomBatch.class);
    }

}
